package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.TreeNode;
import org.springframework.util.Assert;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName BinaryTreeIsSymmetricV1_Bad
 * @Description BinaryTreeIsSymmetricV1_Bad
 * @Author wanghaiwei
 * @Date 2022/11/24 13:49
 */
public class BinaryTreeIsSymmetricV2_Iterator {
    public boolean isSymmetric(TreeNode root) {
        return this.check(root,root);
    }

    private boolean check(TreeNode leftNode, TreeNode rightNode) {
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.offer(leftNode);
        nodeQueue.offer(rightNode);
        while(!nodeQueue.isEmpty()) {
            leftNode = nodeQueue.poll();
            rightNode = nodeQueue.poll();
            if (leftNode == null && rightNode ==null) {
                continue;
            }
            if ((leftNode == null || rightNode == null) || leftNode.val != rightNode.val) {
                return false;
            }
            nodeQueue.offer(leftNode.left);
            nodeQueue.offer(rightNode.right);
            nodeQueue.offer(leftNode.right);
            nodeQueue.offer(rightNode.left);
        }
        return true;
    }


    public static void main(String[] args) {
        BinaryTreeIsSymmetricV2_Iterator solution = new BinaryTreeIsSymmetricV2_Iterator();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);
        Assert.isTrue(solution.isSymmetric(root), "[1,2,2,3,4,4,3]结果计算错误");

        TreeNode root2 = new TreeNode(1);
        root2.left = new TreeNode(2);
        root2.left.right = new TreeNode(3);
        root2.right = new TreeNode(2);
        root2.right.right = new TreeNode(3);
        Assert.isTrue(!solution.isSymmetric(root2), "[1,2,2,null,3,null,3]结果计算错误");

        TreeNode root3 = new TreeNode(1);
        root3.left = new TreeNode(2);
        root3.left.left = new TreeNode(2);
        root3.right = new TreeNode(2);
        root3.right.left = new TreeNode(2);
        Assert.isTrue(!solution.isSymmetric(root3), "[1,2,2,2,null,2]结果计算错误");
    }
}
